package de.axone.service;

import javax.servlet.http.HttpServletRequest;

import de.axone.wash.DefaultWash;
import de.axone.wash.Wash;
import de.axone.wash.Wash.Type;
import de.axone.wash.Wash.WashException;
import de.axone.wash.handler.Handler.OperationNotFoundException;
import de.axone.wash.handler.Handler.WrongParametersException;
import de.axone.wash.service.Service;


public class MyService implements Service {

	public Wash service( Wash request, HttpServletRequest httpRequest ) throws OperationNotFoundException, WrongParametersException {
		
		String operation=null;
		try {
			operation = request.getString( "wash.op" );
			
		} catch( WashException e ) {
			
			throw new OperationNotFoundException();
		}
		
		if( ! "search".equals( operation ) )
			throw new OperationNotFoundException();
		
		Wash result = doSearch( request );

		return result;
	}
	
	private Wash doSearch( Wash request ) {
		
		Wash result = new DefaultWash();
		
		try {
			result.addField( "result", Type.STRING, "Hello World" );
			result.addField( "xzy", Type.STRING, "Klopf!\nHallöchen?" );
			result.addField( "data", Type.DATA, new byte[] { 0x41, 0x58, 0x4f, 0x4e, 0x2d, 0x45 } );
			result.addField( "int", Type.INTEGER, 666 );
			result.addField( "float", Type.FLOAT, 213.45 );
			result.addField( "xxx", Type.FLOAT, 213.45 );
			
			if( request.hasField( "search.q" ) )
				result.addField( "your.query", Type.STRING, request.getString( "search.q" ));
			
		} catch( WashException e ) {}
		
		return result;
	}
}
